	
	
    //Durak script global variables
	var btn = $("#btn");
	var getCardsBtn = $("#getCardsBtn");
	var result = $("#result");
	var getTalonsCardsBtn = $("#getTalonsCards");
	var endGame = $('#endGame');
	var playerTable = "#playerTable";
	var newTable = "<table class='table table-hover' style='overflow:auto;'></table>";
	
	var talonTable = $("#talonCards");
	var revealedTable = $("#revealedCards");
	var playerOneCards = $("#playerOneCards");
	var playerTwoCards = $("#playerTwoCards");
	var tracer = $("#tracer");
	var masterSuit = $('#masterSuit');
	
	var masterSuitInfo = $('#masterSuitInfo');
	var attackerInfo = $('#attackerInfo');
	var defenderInfo = $('#defenderInfo');
	var turnInfo = $('#turnInfo');
	var remainingCardsInfo = $('#remainingCardsInfo');
	
	var newDiv = "<div></div>";
	var newRow = "<tr></tr>";
	var newData = "<td></td>";
	var newHead = "<th></th>";
	var newImage = "<img />";
	var resImagesPathCards = "res/images/durakCards/";
	var newBtn = "<input type='button'  />";
	
	var drawCardp1 = $("#drawCardp1");
	var refeshGame = $("#refeshGame");
	var drawCardp2Btn = $('#drawCardp2');
	var attackp1Btn = $("#attackp1");
	var newGamebtn = $("#newGame");
	var acceptCardsp1 = $('#acceptCardsp1');
	var acceptCardsp2 = $('#acceptCardsp2');
	var endAttackp1 = $('#endAttackp1');
	var endAttackp2 = $('#endAttackp2');
	var attackToBecomeAttackFromDefender1 = $('#attackToBecomeAttackFromDefender1');
	var attackToBecomeAttackFromDefender2 = $('#attackToBecomeAttackFromDefender2');
	var whosDurak = $('#whosDurak');
	
	
	p1 = new Player({'id':1,'name':'ICHI KURUSAKI'});
	p2 = new Player({'id':2,'name':'NARUTO'});
	players_ = new Array();
	players_[0] = p1;
	players_[1] = p2;
	
	game = new Game({'id':0,'':'','end':'','winner_id':'','masterSuit':'','type':'side by side','players':players_});
	table = new Table();
	
	attackToBecomeAttackFromDefender1.hide();
	endAttackp1.hide();
	acceptCardsp1.hide();
	//player two config priv.
	attackToBecomeAttackFromDefender2.hide();
	endAttackp2.hide();
	acceptCardsp2.hide();
	
	endAttackp1.click(function(){
		
		var  val = table.whosTurn;
		console.log('endAttackp1 = '+val);
		if(val!=1){
			alert('its not your turn!');
			return;
		}
		if(game.cardsRevealed.length==0){
			alert('There is no cards revealed');
			return;
		}
		
		if(game.players[0].endAttack==true){
			alert('you just click me a while a go!! please wait till player two turn is over');
			return;
		}
		
		if(game.players[1].iAcceptCards==false){
			var con = confirm('Player 2 did not accept the cards!!... if you continue player two will have the chance to attack or to accept cards');
			if(con){
				//inform player two to accept or attack
				//game.players[0].endAttack = true;
				game.players[0].iEndAttack = true;
				game.players[1].iAcceptCards = true;
				endAttackp1.hide();
				attackToBecomeAttackFromDefender2.show();
				table.whosTurn = 2;
				table.updateGameInformation();
				console.log('waiting for the other player to decide!');
			}
			return;
		}
		
		if(game.players[1].iAcceptCards==true){
			table.whosTurn = 2;
			game.players[1].acceptCards();
			table.whosTurn = 1;
			game.players[0].iAcceptCards = false;
			game.players[1].iEndAttack = false;
			game.players[1].iAcceptCards = false;
			game.players[0].iEndAttack = false;
			table.updateGameInformation();
			
		}
		game.replenishPlayers();
		acceptCardsp2.show();
	});
	
	endAttackp2.click(function(){
		var  val = table.whosTurn;
		console.log('endAttackp2 = '+val);
		if(val!=2){// & !game.players[1].didOtherPlayerAcceptCards()){
			alert('its not your turn!');
			return;
		}
		
		if(game.cardsRevealed.length==0){
			alert('There is no cards revealed');
			return;
		}
		
		if(game.players[1].endAttack==true){
			alert('you just click me a while a go!! please wait till player one turn is over');
			return;
		}
		
		if(game.players[0].iAcceptCards==false){
			var con = confirm('Player one did not accept the cards!!... if you continue player one will have the chance to attack or to accept cards.. continue?');
			if(con){
				//inform player two to accept or attack
				//game.players[1].endAttack = true;
				game.players[1].iEndAttack = true;
				game.players[0].iAcceptCards = true;
				endAttackp2.hide();
				attackToBecomeAttackFromDefender1.show();
				table.whosTurn = 1;
				table.updateGameInformation();
				console.log('waiting for the other player to decide!');
			}
			return;
		}
		
		if(game.players[0].iAcceptCards==true){
			table.whosTurn = 1;
			game.players[0].acceptCards();
			table.whosTurn = 2;
			game.players[0].iAcceptCards = false;
			game.players[1].iEndAttack = false;
			game.players[1].iAcceptCards = false;
			game.players[0].iEndAttack = false;
			table.updateGameInformation();
		}
		
		game.replenishPlayers();
		acceptCardsp1.show();
	});
	
	acceptCardsp1.click(function(){
		game.players[0].acceptCards();
	});
	
//	acceptCardsp2.click(function(){
//		game.players[1].acceptCards();
//	});
	
	endGame.click(function(){
		var con = confirm('are you sure to end your life now? :D !!!!')
		if(!con){
			return;
		}
		game.endGame();
		//window.location = "index.php?/player";
	});
	
	jQuery.fn.extend({
		hide: function(){
			this.css('display','none');
		},
		show: function(){
			this.css('display','');
		}
	});
	
	newGamebtn.click(function(){
		newGamebtn.hide();
		var val = Math.floor(Math.random() * 2) + 1;
		//("random value  = "+val);
		if(val==1){
			table.whosAttackerId = 1;
			table.whosDefenderId = 2;
			table.whosTurn = 1;
		}else{
			table.whosAttackerId = 2;
			table.whosDefenderId = 1;
			table.whosTurn = 2;
		}
		
		$.when(game.createNewGameAJAX()).done(function(){
			table.addTracerInfo(new Info({'description':'qcreateNewGameAJAX','triggered':'createNewGameAJAX()'}));
			
			$.when(game.getCurrentGameId()).done(function(){
				table.addTracerInfo(new Info({'description':'getCurrentGameId','triggered':'getCurrentGameId()'}));
				
				$.when(game.getNewSetOfShuffledCards()).done(function(){
					table.addTracerInfo(new Info({'description':'getNewSetOfShuffledCards','triggered':'getNewSetOfShuffledCards()'}));
					
					$.when(game.getAllCardsAJAX()).done(function(){
						table.addTracerInfo(new Info({'description':'getAllCardsAJAX','triggered':'getAllCardsAJAX()'}));
						
						$.when(game.getTalonCardsByGameId()).done(function(){
							table.addTracerInfo(new Info({'description':'getTalonCardsByGameId','triggered':'getTalonCardsByGameId()'}));
							table.reLoadTalon();
							table.reLoadPlayersTable(1);
							table.reLoadPlayersTable(2);
							game.getRevealedCards();
							table.reloadRevealedTableNoAjax();
							
							// simulate replenishing player cards then after both player has six cards 
							//simulate new master suit. update DB and inform game object and other concerns objects
							game.drawCardTo(1);
							game.drawCardTo(2);
							
							game.drawCardTo(1);
							game.drawCardTo(2);
							
							game.drawCardTo(1);
							game.drawCardTo(2);
							
							game.drawCardTo(1);
							game.drawCardTo(2);
							
							game.drawCardTo(1);
							game.drawCardTo(2);
							
							game.drawCardTo(1);
							game.drawCardTo(2);
							
							game.drawCardTo(1);
							game.drawCardTo(2);
							// creating master suit.....
							game.createNewMasterSuit();
							table.reloadMasterSuit();
							table.updateGameInformation();
							table.updatePlayerPrivileges();
//							$.when(game.re.loadUI()).done(function(){
//								table.addTracerInfo(new Info({'description':'reloadUI','triggered':'reloadUI()'}));
//								
//							});
						});	
					});	
				});	
			});	
		});
	});
	
	Array.prototype.updateReplenishee_Id = function(talonId,playerId__){
		for(i=0;i<this.length;i++){
			if(Number(this[i].id) == Number(talonId)){
				this[i].replenishee_id = playerId__;
				this[i].revealed = false;
				return;
			}
		}
	}
	
	Array.prototype.getRemainingTalonCards = function(){
		var card = []
		for(i=0;i<this.length;i++){
			if(this[i].replenishee_id == 0 | this[i].replenishee_id == '0'){
				if(this[i].revealed!=true){
					card.push(this[i]);
				}
			}
		}
		return card;
	}
	
	// used for players cards to sort 
	Array.prototype.sortCardByRanking = function(){
		var sortCards = [];
		var lastCardRanking = {'card_id':0};
		var playerCardsLength = this.length;
		var masterSuit = [];
		
		while(playerCardsLength>0){
			for(card_=0;card_<this.length;card_++){
				var isThisGuyOneOfTheCards = false;
				for(lcr=0;lcr < sortCards.length; lcr++){
					if(Number(sortCards[lcr].card_id)==Number(this[card_].card_id)){
						isThisGuyOneOfTheCards = true;
						break;
					}
				}
				var isThisMasterSuit = false;
				if(!isThisGuyOneOfTheCards){
					var theMasterSuit = game.talonCards.getMasterSuit();
					var thisCard = this[card_];
					
					
					var tmsv = game.cards.getSuit(theMasterSuit.card_id);
					var tcv = game.cards.getSuit(thisCard.card_id);
					if(tmsv==tcv){
						isThisMasterSuit = true;
						if(masterSuit.length==0){
							masterSuit.push(this[card_]);
						}
						var isTrue = false;
						for(xx=0;xx<masterSuit.length;xx++){
							if((Number(masterSuit[xx].id)==Number(this[card_].id))){
								isTrue = true;
								break;
							}
						}
						if(!isTrue && masterSuit.length>1){
							masterSuit.push(this[card_]);
						}
					}
				}
				
				if(!isThisMasterSuit && Number(this[card_].card_id) > Number(lastCardRanking.card_id) && !isThisGuyOneOfTheCards){
					lastCardRanking = this[card_];
				}
				
				isThisGuyOneOfTheCards = false;
			}
			if(lastCardRanking.card_id!=0){
				sortCards.push(lastCardRanking);
			}
			lastCardRanking = {'card_id':0};
			playerCardsLength--;
		}
		
		var masterSuitLength = masterSuit.length;
		var sortedMasterSuits = [];
		var lastCardMasterIn = {'card_id':0};
		while(masterSuitLength>0){
			for(card__=0;card__<masterSuit.length;card__++){
				var isThisGuyOneOfTheCards = false;
				for(lcr_=0;lcr_ < sortedMasterSuits.length; lcr_++){
					if(Number(sortedMasterSuits[lcr_].card_id)==Number(masterSuit[card__].card_id)){
						isThisGuyOneOfTheCards = true;
						break;
					}
				}
				if(Number(masterSuit[card__].card_id) > Number(lastCardMasterIn.card_id) && !isThisGuyOneOfTheCards){
					lastCardMasterIn = masterSuit[card__];
				}
			}
			
			sortCards.unshift(lastCardMasterIn);
			
			lastCardMasterIn = {'card_id':0};
			masterSuitLength--;
		}
		
		
		return sortCards;
	}
	
	Array.prototype.updateMasterSuit = function(masterSuit){
		for(i=0;i<this.length;i++){
			if(this[i].id == masterSuit.id){
				this[i].masterSuit = true;
			}
		}
				
	}
	
	Array.prototype.updateCardsRevealed = function(talonId){
		for(i=0;i<this.length;i++){
			if(this[i].id == talonId){
				this[i].revealed = 0;
				this[i].replenishee_id = 0;
			}
		}
	}
	
	Array.prototype.isAlreadyInPlayerTable = function(talonId){
		for(i=0;i<this.length;i++){
			if(this[i].id == talonId){
				console.log('isAlreadyInPlayerTable true');
				return true;
			}
		}
		return false;
	}
	
	Array.prototype.isInPlayerTable = function(talonId,playerId){
		var rows = playerId==1 ? $('#playerOneCards tr') : $('#playerTwoCards tr');
		$(rows).each(function(key,value){
			if($(this).attr('id')===talonId){
				return true;
			}
		});
		return false;	
	}
	Array.prototype.getSuit  = function(cardId){
		console.log('getSuit -----');
		for(i_=0;i_<this.length;i_++){
			if(Number(this[i_].id)==Number(cardId)){
				console.log('suit aquired');
				return this[i_].suit;
			}
		}
	}
	Array.prototype.removeRow = function(talonId){
		for(i=0;i>this.length;i++){
			if(this[i].id == talonId){
				$(this[i]).remove();
			}
		}
	}
	
	//this is not used... will be deleted sooner
// attackp1Btn.click(function(){
//		if(table.whosTurn!=1){
//			console.log('its not yout turn please wait....!');
//			return;
//		}
//		
//		if(game.players[0].cards.length != 0){
//			$.when(game.players[0].drawCard()).then(function(e){
//				console.log('player 1 drawn a card');
//			});
//		}else{
//			alert("No more card attack.. youve reach your limit!");
//		}
//	});
	
	drawCardp2Btn.click(function(){
		if(game.players[1].cards.length < 7){
			$.when(game.players[1].drawCard()).then(function(e){
				console.log('player 2 drawn a card');
			});
		}else{
			alert("No more card draw.. youve reach your limit!");
		}
	});
	
	refeshGame.click(function(){
		game.getCurrentGameId();
		game.refreshGame();
		game.getRevealedCards();
		table.reloadRevealedTableNoAjax();
		table.reloadMasterSuit();
		table.updateGameInformation();
		table.updatePlayerPrivileges();
		//table.updatePlayerPrivileges();
	});
	
	
	
	drawCardp1.click(function(){
		if(game.players[0].cards.length < 7){
			$.when(game.players[0].drawCard()).then(function(e){
				console.log('player 1 drawn a card');
			});
		}else{
			alert("No more card draw.. youve reach your limit!");
		}
	});
	
	Array.prototype.getActiveTalonCards = function(){
		var cards = [];
		for(i=0;i<this.length;i++){
			if(Number(this[i].replenishee_id) == 0 | this[i].revealed==false | Number(this[i].revealed)==0){
				cards.push(this[i]);
			}
		}
		return cards;
	}
	
	
	Array.prototype.updateTalonCardRevealed = function(talonId){
		for(i=0;i<this.length;i++){
			if(this[i].id==talonId){
				console.log('updateTalonCardRevealed = true');
				this[i].revealed = true;
				console.log('this[i].replenishee_id = '+this[i].replenishee_id);
				this[i].replenishee_id = 0;
				return this[i];
			}
		}
	}
	
	Array.prototype.getMasterSuit = function(){
		for(i=0;i<this.length;i++){
			if(this[i].masterSuit=='1' | this[i].masterSuit==1 | this[i].masterSuit==true | this[i].masterSuit=='true'){
				console.log('getMasterSuit---- true');
				return this[i];
			}
		}
	}
	
	Array.prototype.getCardByTalonId_ = function(card_id){
		for(i=0;i<this.length;i++){
			if(this[i].id==card_id){
				return this[i];
			}
		}
	}
	
	Array.prototype.checkFaceValue = function(newtalonCard){
		var cardValue = game.cards.getCardByTalonId_(newtalonCard.card_id);
		for(c=0;c<this.length;c++){
			console.log(c);
			for(x=0;x<game.talonCards.length;x++){
				if(this[c].talon_id==game.talonCards[x].id){
					var getCard = game.cards.getCardByTalonId_(game.talonCards[x].card_id);
					if(getCard.rank==cardValue.rank){
						return true;
					}
				}
			}
		}
		return false;
	}
	
	Array.prototype.getCardById = function(talonId){
		for(i=0;i<this.length;i++){
			if(this[i].id==talonId){
				return this[i];
			}
		}
	}
	
	Array.prototype.getCardsbyPlayerId = function(playerId){
		var cards = [];
		for(i=0;i<this.length;i++){
			if(this[i].replenishee_id==playerId){
				if(this[i].revealed == '0' | this[i].revealed == 0){
					cards.push(this[i]);
					console.log('getCardsbyPlayerId = true');
				}
			}
		}
		return cards;
	}
	
	Array.prototype.updateTalonCard = function(talonCardId,replenishee_id){
		console.log('updateTalonCard');
		for(i=0;i<this.length;i++){
			if(this[i].id==talonCardId){
				console.log('true');
				this[i].replenishee_id = replenishee_id;
				return;
			}
		}
	}
	
	Array.prototype.getLength = function(){
		return this.length;
	}
	
	Array.prototype.exist = function(talonId){
		for(i=0;i<this.length;i++){
			if(this[i].id==talonId){
				return true;
			}
		}
		return false;
	}
	
	Array.prototype.findPlayerById = function(playerId){
		for(i=0;i<this.length;i++){
			console.log(this[i].id);
			if(this[i].id==playerId){
				return this[i];
			}
		}
	};
	
	Array.prototype.findPlayerCardByTalonid = function(talonId){
		for(i=0;i<this.length;i++){
			console.log(this[i].id);
			if(this[i].id==talonId){
				return this[i];
			}
		}
	}
	
	Array.prototype.updateImg = function(cardId, talonId){
		for(i=0;i<game.cards.length;i++){
			if(!(game.cards[i].id == cardId )){
				break;
			}
			for(o=0;o<this.length;o++){
				if(talonId==this[o].id ){
					console.log('updateImg = true');
					this[o].imageFilName = game.cards[i].image;
				}
			}
		}
	}
	
	Array.prototype.updateRevealedCards = function(revealedCards){
		for(i=0;i<this.length;i++){
			for(o=0;o<revealedCards.length;o++){
				if(this[i].id == revealedCards[o].talon_id){
					this[i].active==false;
				}
			}
		}
	}
	
	
	Array.prototype.updateRevealedCardsImageSrc = function(){
		console.log('updateRevealedCardsImageSrc-------------');
		for(i=0;i<this.length;i++){
			for(o=0;o<game.talonCards.length;o++){
				if(this[i].talon_id==game.talonCards[o].id){
					var cardId = game.talonCards[o].card_id;
					for(x=0;x<game.cards.length;x++){
						if(game.cards[x].id==cardId){
							this[i].imageFilName = game.cards[x].image;
						}
					}
				}
			}	
		}
	}
	
	Array.prototype.finCardsRevealedByTalonId = function(talonId){
		for(i=0;i<this.length;i++){
			console.log(this[i].id);
			if(this[i].id==talonId){
				return this[i];
			}
		}
	}
	
	Array.prototype.findByTalonId = function(talonId){
		for(i=0;i<this.length;i++){
			console.log(this[i].id);
			if(this[i].id==talonId){
				return this[i];
			}
		}
	}
	
	btn.click(function(event){
		$.each(game.players,function(key,player){
			player.info;
		});
		var result = game.players.findPlayerById(1);
		alert(result);
	});
	
	getCardsBtn.click(function(event){
		getAllCards();
	});
	
	getTalonsCardsBtn.click(function(event){
		getAllTalonCardByGameId(); // game id should be fetch form php session.
	});